﻿//-----------------------------------------------------------------------
// <copyright file="SendPrivateMessageCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//-----------------------------------------------------------------------

namespace ChatBox.UI.Windows.SendPrivateMessage
{
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.Utility;

    using DomainObjects.ResultData;

    /// <summary>
    /// A command to send a message to the server
    /// </summary>
    public class SendPrivateMessageCommand : GenericCommandBase<SendPrivateMessageViewModel>
    {
        /// <summary>
        /// The service to use to send messages
        /// </summary>
        private readonly IChatMessageSend sendService;

        /// <summary>
        /// The view model in use
        /// </summary>
        private SendPrivateMessageViewModel currentViewModel;

        /// <summary>
        /// Initializes a new instance of the SendPrivateMessageCommand class
        /// </summary>
        /// <param name="sendService">The service to use to send messages</param>
        public SendPrivateMessageCommand(IChatMessageSend sendService)
        {
            this.sendService = sendService;
            this.sendService.Completed += this.MessageSendCompleted;
        }

        /// <summary>
        /// Execute the send command
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(SendPrivateMessageViewModel value)
        {
            if (value == null)
            {
                return;
            }

            this.currentViewModel = value;

            if (this.SendMessage(value))
            {
                value.Window.Close();
            }
        }

        /// <summary>
        /// Test if the command can execute
        /// </summary>
        /// <param name="value">the view model to test</param>
        /// <returns>true if the command can execute</returns>
        public override bool CanExecuteOnValue(SendPrivateMessageViewModel value)
        {
            if (value == null)
            {
                return false;
            }

            // can execute when there is text to send
            return value.MessageText.HasContent();
        }

        /// <summary>
        /// Send the message
        /// </summary>
        /// <param name="viewModel">the view model to work upon</param>
        /// <returns>false if sending fails, true if all looks ok</returns>
        private bool SendMessage(SendPrivateMessageViewModel viewModel)
        {
            if (viewModel.MessageText.IsNullEmptyOrWhiteSpace())
            {
                return false;
            }

            this.sendService.SendMessageTo(viewModel.MessageRecipient.Text, viewModel.MessageText.Trim());
            return true;
        }

        /// <summary>
        /// Send completed handler
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the vent params</param>
        private void MessageSendCompleted(object sender, EventArgs<OperationResult> e)
        {
            // notify the main window
            if (this.currentViewModel.OperationCompletedCommand != null)
            {
                this.currentViewModel.OperationCompletedCommand.Execute(e.Value);  
            }
        }
    }
}
